package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    //1.接收用户信息 利用MP完成新增
    //入库操作
    /*@PostMapping("/addUser")
    public String addUser(User user){
        userService.addUser(user);
        return "新增成功";
    }*/

    @GetMapping("/getUser")
    public List<User> getUser(){
        List<User> user = userService.getUser();
        return user;
    }


    @GetMapping("/user/{id}")
    public User getUser(@PathVariable Integer id){
        return userService.getUserById(id);
    }

    @PostMapping("user/{name}/{age}/{sex}")
    public String addUserRest(User user){
        userService.addUser(user);
        return "入库成功!!";
    }


    //根据id查询数据
    @GetMapping("/getUserById")
    public User getUserById(Integer id){
        return userService.getUserById(id);
    }



    @DeleteMapping("/user/{id}")
    public String deleteUser(@PathVariable Integer id){
        userService.deleteUser(id);
        return "数据删除成功";
    }

    /*
    * 接收post请求
    * 传递的参数时一个json串
    * 解决方案:可以将json的数据根据key:value转化为对象(get/set方法)
    * */
    @PostMapping("/addUser")
    public String addUser(@RequestBody User user){
        System.out.println(user);
        userService.addUser(user);
        return "新增用户成功";
    }

    @PostMapping("/addUserForm")
    public String addUserForm( User user){
        System.out.println(user);
        userService.addUser(user);
        return "新增用户成功!!";
    }



   @PutMapping("user/{id}/{name}")
    public String updateUser(User user){
        userService.updateUser(user);
        return "修改成功!!";
    }
    /*
    * 实现修改操作
    * 参数:user的json数据
    * */


    @PutMapping("/updateUser")
    public String updateUser2( @RequestBody User user){

        userService.updateUser(user);
        return "实现数据修改";
    }
}
